package com.sw.chinesewriteboard.activity;

import android.annotation.SuppressLint;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sw.chinesewriteboard.R;
import com.sw.chinesewriteboard.config.ChineseDataConfig;
import com.sw.chinesewriteboard.config.EnglishDataConfig;
import com.sw.chinesewriteboard.copybook.EnCopyBookActivity;
import com.sw.chinesewriteboard.model.EnglishData;
import com.sw.chinesewriteboard.operation.EnglishDataOperate;

/**
 * @Description:
 * @author: GordenGao
 * @Email: gordengao124@gmail.com
 * @date: 2021年06月23日 16:12
 */
public class EnWordCopybookActivity extends EnCopyBookActivity {

    @Override
    protected void initOwnView() {
        mEnglishDataOperate.setArrangeType(EnglishDataOperate.ArrangeType.WORD);

        binding.vbnBottomNavigation.initEnvironment(this, false);
//        setTitle("英文单词帖");
        binding.vbnBottomNavigation.setContantHint("请输入生成字帖的单词（单词之间以空格分隔）。例如：good monring");
        binding.vbnBottomNavigation.findViewById(R.id.vbn_ziti_ll_zihao).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_ziti_ll_alpha).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_gezi_csl_kuandu).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_gezi_csl_miao).setVisibility(View.GONE);
    }

    protected void refreshCopyBookView() {

        int COLUMN_LENGTH = EnglishDataConfig.TOTAL_LENGTH_COLUMN;
        int ROW_LENGTH = EnglishDataConfig.TOTAL_LENGTH_ROW;

        mWordPaper = new LinearLayout(this);
        mWordPaper.setLayoutParams(new LinearLayout.LayoutParams(ChineseDataConfig.getA4width(this), ChineseDataConfig.getA4height(this)));
        mWordPaper.setGravity(Gravity.CENTER);
        mWordPaper.setAlpha(0f);
        binding.getRoot().addView(mWordPaper);

        mWordList = new LinearLayout(this);
        mWordList.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
        mWordList.setOrientation(LinearLayout.VERTICAL);
        mWordList.removeAllViews();

        mEditTextList.clear();


        mData.setText(mEnglishDataOperate.listToString(mListContent));

        int startIndex = EnglishDataConfig.TOTAL_LENGTH * mPage;    // 字符串的起始坐标
        int k = startIndex;
        System.out.println("日志：p：" + mPage + "    k:" + k);

        for (int i = 0; i < ROW_LENGTH; i++) {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
            row.setOrientation(LinearLayout.HORIZONTAL);

            View view;
            String text;
            if (mListContent.size() == 0) {
                text = "";
            } else {
                String[] strings = mListContent.get(0).split(" ");
                if (i < strings.length) {
                    text = strings[i];
                } else {
                    text = "";
                }
            }
            view = createCharacterLayout(text, mData);
            view.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
            row.addView(view);
            k++;

            mWordList.addView(row);
        }
        mWordPaper.addView(mWordList);

        binding.cbIvCopybook.post(new Runnable() {
            @Override
            public void run() {
                resultBitmap = loadBitmapFromView();
                binding.cbIvCopybook.setImageBitmap(resultBitmap);
                binding.getRoot().removeView(mWordPaper);
            }
        });
    }

    @SuppressLint("SetTextI18n")
    protected View createCharacterLayout(String s, EnglishData data) {
        int gridWidth = EnglishDataConfig.getGridWidth(EnWordCopybookActivity.this);
        int gridHeight = EnglishDataConfig.getGridHeight(EnWordCopybookActivity.this);

        LinearLayout characterLayout = new LinearLayout(this);
        characterLayout.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
        characterLayout.setOrientation(LinearLayout.VERTICAL);

        final TextView character = new TextView(this);
        character.setGravity(Gravity.CENTER_VERTICAL);
        character.setPadding(gridHeight, 0, gridHeight, 0);
        LinearLayout.LayoutParams characterParams;
        characterParams = new LinearLayout.LayoutParams(gridWidth * EnglishDataConfig.TOTAL_LENGTH_COLUMN, gridHeight);
        characterParams.bottomMargin = gridHeight;
        character.setEnabled(false);
        character.setText(s);
        mCharacterOperation.setCharacterAppearance(character, data);
        mEditTextList.add(character);
        characterLayout.addView(character, characterParams);

        return characterLayout;
    }
}
